package com.tianyi.sfzy1.controller;

import com.github.pagehelper.PageInfo;
import com.tianyi.sfzy1.domain.Member;
import com.tianyi.sfzy1.service.MemberService;
import com.tianyi.sfzy1.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
//    会员登录
    @GetMapping("/login")
    public R login(String username, String password) {
        int i = memberService.login(username, password);
        if (i == 1){
            //登录成功
            return R.ok(UUID.randomUUID());
        }else {
            return R.fail("用户名或密码错误");
        }
    }

//    会员注册
    @PostMapping("/register")
    public R register(Member member) {
        int i = memberService.register(member);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail("注册失败");
        }
    }

    //根据用户名或电话查询会员信息
    @GetMapping("/username")
    public R selectByUsername(String username) {
        Member member = memberService.selectByUsername(username);
        if (member != null){
            return R.ok(member);
        }else {
            return R.fail("用户名或电话不存在");
        }
    }

//    根据用户名修改会员手机号
    @PutMapping("/username")
    public R updatePhoneByUsername(String username, String mobile) {
        int i = memberService.updatePhoneByUsername(username, mobile);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail("修改失败");
        }
    }

//    根据用户名修改会员密码
    @PutMapping("/password")
    public R updateByUsername(String username, String password) {
        int i = memberService.updateByUsername(username, password);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail("修改失败");
        }
    }

//    查询所有会员信息
    @GetMapping("/all")
    public R findAll(){

        List<Member> list=memberService.findAll();
        return R.ok(list);
    }

    // 查询所有会员信息，包含会员积分
    @GetMapping("/allWithPoints")
    public R findAllWithPoints() {
        List<Member> list = memberService.findAllWithPoints();
        if (list!=null){
            return R.ok(list);
        }else {
            return R.fail("查询失败");
        }
    }

    //查询所有会员信息，包含会员积分(分页)
    @GetMapping("/allWithPointsPage")
    public R findAllWithPointsPage(int pageNum, int pageSize) {
        PageInfo pageInfo = memberService.findAllWithPointsPage2(pageNum, pageSize);
        if (pageInfo!=null){
            return R.ok(pageInfo);
        }else {
            return R.fail("查询失败");
        }
    }

    //查询所有会员的数量
    @GetMapping("/selectCount")
    public R selectCount(){
        int i = memberService.selectCount();
        if (i > 0){
            return R.ok(i);
        }else {
            return R.fail("查询失败");
        }
    }

    //根据id查询会员预约咨询师信息
    @GetMapping("/consultationappointment")
    public R findAllConsultationappointment(int id){
        List<Member> list=memberService.findAllConsultationappointment(id);
        return R.ok(list);
    }

    //查询所有预约信息
    @GetMapping("/consultation")
    public R findAllConsultation(){
        List<Member> list=memberService.findAllConsultation();
        return R.ok(list);
    }

    @GetMapping("/findConsultation")
    public R findConsultation(String name){
        List<Member> list=memberService.findConsultation(name);
        return R.ok(list);
    }

//    插入会员信息
    @PostMapping("/insertMember")
    public R insertMember(Member member){
        int i = memberService.insertMember(member);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail("插入失败");
        }
    }

//    根据用户名查询会员信息
    @GetMapping("/username1")
    public R selectByUsername1(String username){
        Member member=memberService.selectByUsername1(username);
        if (member!=null){
            return R.ok(member);
        }else {
            return R.fail("查询失败");
        }
    }

//    根据用户名修改会员信息
    @PutMapping("/username2")
    public R updateByUsername1(Member member){
        int i = memberService.updateByUsername1(member);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail("修改失败");
        }
    }

    //根据用户名删除会员信息
    @DeleteMapping("/username3")
    public R deleteByUsername(String username){
        int i = memberService.deleteByUsername(username);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail("删除失败");
        }
    }

    @GetMapping("/findconsultationappointment")
    public R findconsultationappointment(int mid){
        List<Member> list=memberService.findconsultationappointment(mid);
        return R.ok(list);
    }

//    根据会员姓名查询会员信息(模糊查询)
    @GetMapping("/selectByUsername2")
    public R selectByUsername2(String realname){
        List<Member> list=memberService.selectByUsername2(realname);
        return R.ok(list);
    }

    //根据会员编号删除会员信息(批量删除)
    @DeleteMapping("/deleteByIds")
    public R deleteByIds(@RequestBody List<Integer> memberId){
        int i = memberService.deleteByIds(memberId);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail("删除失败");
        }
    }
}
